package edu.pku.sei.mte.mtemodel.mtmodel.pattern;

import java.util.ArrayList;
import java.util.List;

import edu.pku.sei.mte.mtemodel.graph.Graph;
import edu.pku.sei.mte.mtemodel.mtmodel.expression.Expression;
import edu.pku.sei.mte.runtime.Context;
import edu.pku.sei.mte.utilities.Visitor;

public class TPatternGraph extends Graph<TPatternNode,TPatternEdge> {
	private List<Expression> templateExpressions;
	static private int unnamedCount = 0;
	static public String allocateNameFor(Context context){
		return "__UNNAMED__"+(unnamedCount++);
	}
	
	public List<Expression> getTemplateExpressions(){
		if(templateExpressions==null){
			templateExpressions = new ArrayList<Expression>();
		}
		return templateExpressions;
	}
	
	public void registVariableToContext(Context context){
		for(TPatternNode pn : getNodes()){
			if(pn.getName()==null)
				pn.setName(allocateNameFor(context));
			context.registModelVariable(pn, pn.getType());
		}
		for(TPatternEdge pe : getEdges()){
			if(pe.getName()==null)
				pe.setName(allocateNameFor(context));
			context.registModelVariable(pe, pe.getType());
		}
	}
	
	public boolean containsNode(String name){
		for(TPatternNode n : getNodes()){
			if(n.getName().equals(name))
				return true;
		}
		return false;
	}
	
	@Override
	public Object accept(Visitor visitor) {
		return visitor.visitTPatternGraph(this);
	}
}
